package com.example.mychat.controller;

import com.example.mychat.entity.Image;
import com.example.mychat.service.ImageService;
import com.example.mychat.service.impl.ImageServiceImpl;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@ResponseBody
@RequestMapping("/image")
@Tag(name = "图片接口")
public class ImageController {
    @Autowired
    private ImageServiceImpl imageService;

    //从数据库查询获得json数据
    @GetMapping("/queryAll")
    @Operation(summary = "查询全部图片信息")
    public List<Image> queryAll(){
        return imageService.findAllImage();
    }

    @PostMapping("/insert")
    @Operation(summary = "增加新的图片")
    public int insert(@RequestParam("image")@Parameter(name = "image", description = "PNG、JPEG、GIF三选一")  MultipartFile file) {
        if (file==null) {
            return -1;
        }
        Image newImage = imageService.prepImageSaveToDB(file);
        if(newImage!=null){
            if(imageService.insertImage(newImage)>0){
                return newImage.getId();
            }
        }
        return -1;
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查找图片")
    public ResponseEntity<byte[]> getImageById(
            @PathVariable("id")@Parameter(name = "id", description = "图片ID")Integer id) {
        Image imageById = imageService.getImageById(id);
        if (imageById == null) {
            // 如果找不到图片，返回一个404响应
            return ResponseEntity.notFound().build();
        }
        return imageService.prepareImageResponseEntity(imageById);
    }

    @PostMapping("/update")
    @Operation(summary = "更新图片")
    public int update(
            @RequestParam("image")@Parameter(name = "image", description = "PNG、JPEG、GIF三选一") MultipartFile file,
                      @RequestParam("id")@Parameter(name = "id", description = "图片ID")Integer id){
        if(file == null || id == null || id < 0){
            return -1;
        }
        Image newImage = imageService.prepImageSaveToDB(file);
        if(newImage!=null){
            newImage.setId(id);
            if(imageService.updateImage(newImage)>0){
                return newImage.getId();
            }
        }
        return -1;
    }

    @GetMapping("/del")
    @Operation(summary = "删除图片")
    @Hidden
    public int deleteImageById(@Parameter(name = "id", description = "图片ID")Integer id){
        if (id==null){
            return -1;
        }
        return imageService.delImage(id);
    }
}
